package org.shj.pattern.command

import scala.collection.mutable.ListBuffer

class Request{ //命令模式中的Receiver
  def buy(){
    println("buy command")
  }
  
  def sell(){
    println("sell command")
  }
}

abstract class Command {
  def execute()
}

class BuyCommand(val request: Request) extends Command{
  override def execute(){
    request.buy()
  }
}

class SellCommand(val request: Request) extends Command{
  override def execute(){
    request.sell()
  }
}

class Broker {
  val list = new ListBuffer[Command]()
  
  /**
   * 根据业务需求，根据实际条件，可以实现撤销，重做等命令，客户端只需要关注具体请求
   */
  def addCommand(command: Command){
    list += command
  }
  
  def executeCommands(){
    for(cmd <- list){
      cmd.execute()
    }
  }
}